package com.jz.yanshe.ui;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.jz.jd.rest.business.RestProxy;
import com.jz.jd.rest.response.MemberResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.com.feierman.R;
import com.jz.yanshe.base.BaseActivity;
import com.jz.yanshe.util.PreferencesHelper;

/**
 * Created by Administrator on 2018/4/4.
 */

public class TeamActivity extends BaseActivity implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.swipe_target)
    RecyclerView recyclerView;
    @BindView(R.id.swipeToLoadLayout)
    SwipeToLoadLayout swipeToLoadLayout;
    @BindView(R.id.tv_title)
            TextView tv_title;
    MyAdapter myAdapter = new MyAdapter();
    List<MemberResponse.Member> list = new ArrayList<>();
    int page = 1;
    int type;

    @Override
    protected void initViews() {
        recyclerView.setAdapter(myAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = 2;
            }
        });
        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);
    }

    @Override
    protected void initData() {
        type = getIntent().getIntExtra("type", 0);
        if (type == 1) {
            getSonMember(page + "", "10");
            tv_title.setText("直属团队");
        }else if(type ==2){
            getGrandSonMember(page+"","10");
            tv_title.setText("底层团队");
        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_team;
    }

    @Override
    public void onLoadMore() {
        if (type == 1) {
            page++;
            getSonMember(page + "", "10");
        }else if(type ==2){
            page++;
            getGrandSonMember(page+"","10");
        }

    }

    @Override
    public void onRefresh() {
        if (type == 1) {
            page = 1;
            getSonMember(page + "", "10");
        }else if(type ==2){
            page = 1;
            getGrandSonMember(page+"","10");
        }
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {


        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Holder(getLayoutInflater().inflate(R.layout.item_team, parent, false));
        }

        @Override
        public void onBindViewHolder(Holder holder, int position) {
            holder.tv_nickname.setText(list.get(position).nickname);
            holder.tv_grade.setText(list.get(position).vip_grade_name);
            holder.tv_user_phone.setText(list.get(position).phone);
        }


        @Override
        public int getItemCount() {
            return list.size();
        }


        class Holder extends RecyclerView.ViewHolder {
            TextView tv_nickname, tv_grade, tv_user_phone;

            public Holder(View itemView) {
                super(itemView);
                tv_nickname = itemView.findViewById(R.id.tv_nickname);
                tv_grade = itemView.findViewById(R.id.tv_grade);
                tv_user_phone = itemView.findViewById(R.id.tv_user_phone);
            }
        }
    }


    private void getSonMember(String page, String pagesize) {
        RestProxy.getInstance().getSonMember(PreferencesHelper.getInstance().getString(this, "token"), page, pagesize, new Observer<MemberResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MemberResponse memberResponse) {
                if (TeamActivity.this.page == 1) {
                    list.clear();
                }
                list.addAll(memberResponse.listM);
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
    }

    private void getGrandSonMember(String page, String pagesize) {
        RestProxy.getInstance().getGrandSonMember(PreferencesHelper.getInstance().getString(this, "token"), page, pagesize, new Observer<MemberResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(MemberResponse memberResponse) {
                if (TeamActivity.this.page == 1) {
                    list.clear();
                }
                list.addAll(memberResponse.listM);
                myAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }

            @Override
            public void onComplete() {
                swipeToLoadLayout.setRefreshing(false);
                swipeToLoadLayout.setLoadingMore(false);
            }
        });
    }
}
